﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Security.Policy;
using System.Reflection;
using System.ComponentModel;
using AuditModel;
using AuditWeb.Models;

namespace AuditWeb
{
    public static class Helpers
    {
        public static MvcHtmlString Image(this HtmlHelper html, string src, string alt, int height)
        {
            var url=new UrlHelper(html.ViewContext.RequestContext);

            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(src));
            imgBuilder.MergeAttribute("alt", alt);
            imgBuilder.MergeAttribute("height", height.ToString());
            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            return MvcHtmlString.Create(imgHtml);
        }

        public static List<Subject> GetActiveSubjects(string annualeventid)
        {
            //var subjects;
            using (AuditDBEntities db = new AuditDBEntities())
            {
               var subjects = from s in db.Subjects
                               where (from c in db.CandidateQuotas where c.AnnualEventID==annualeventid select c.SubjectID).Contains(s.Id)
                               select s;

                return subjects.ToList();
            }           
        }
            

        public static MvcHtmlString Link(this HtmlHelper html, string link, string text, string target)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            var linkBuilder = new TagBuilder("a");
            linkBuilder.MergeAttribute("href", url.Content(link));
            linkBuilder.MergeAttribute("target", target);
            string Html = linkBuilder.ToString(TagRenderMode.StartTag)+text+linkBuilder.ToString(TagRenderMode.EndTag);

            return MvcHtmlString.Create(Html);
        }

        public static string getDisplayName(MemberInfo mi)
        {

            object[] atts = mi.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);
           if (atts.Length > 0)
            {
                System.ComponentModel.DataAnnotations.DisplayAttribute da = atts[0] as System.ComponentModel.DataAnnotations.DisplayAttribute;
                return da.GetName();
            }
            return mi.Name;
        }
    }
}